from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from decimal import Decimal
from app.models.competition import CompetitionType, CompetitionStatus, DifficultyLevel


class CompetitionBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=200)
    description: Optional[str] = None
    short_description: Optional[str] = Field(None, max_length=500)
    type: CompetitionType
    category: Optional[str] = None
    tags: List[str] = []
    difficulty: DifficultyLevel = DifficultyLevel.INTERMEDIATE
    platform: Optional[str] = None
    external_url: Optional[str] = None
    prize_pool: Optional[str] = None
    prize_currency: str = "USD"
    prize_amount: Optional[Decimal] = None
    max_team_size: int = 5
    min_team_size: int = 1
    max_participants: Optional[int] = None
    rules: Optional[str] = None
    evaluation_criteria: Optional[str] = None
    resources: List[str] = []


class CompetitionCreate(CompetitionBase):
    registration_start: Optional[datetime] = None
    registration_end: Optional[datetime] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    submission_deadline: Optional[datetime] = None


class CompetitionUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    description: Optional[str] = None
    short_description: Optional[str] = Field(None, max_length=500)
    tags: Optional[List[str]] = None
    difficulty: Optional[DifficultyLevel] = None
    prize_pool: Optional[str] = None
    prize_amount: Optional[Decimal] = None
    max_team_size: Optional[int] = None
    min_team_size: Optional[int] = None
    max_participants: Optional[int] = None
    rules: Optional[str] = None
    evaluation_criteria: Optional[str] = None
    resources: Optional[List[str]] = None
    registration_start: Optional[datetime] = None
    registration_end: Optional[datetime] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    submission_deadline: Optional[datetime] = None
    status: Optional[CompetitionStatus] = None
    is_featured: Optional[bool] = None
    is_public: Optional[bool] = None


class CompetitionResponse(CompetitionBase):
    id: int
    slug: str
    status: CompetitionStatus
    participants_count: int
    teams_count: int
    submissions_count: int
    image_url: Optional[str] = None  # 原始图片URL
    banner_image: Optional[str] = None  # 处理后的图片URL
    logo_image: Optional[str] = None
    gallery_images: List[str] = []
    is_featured: bool
    is_public: bool
    view_count: int
    registration_start: Optional[datetime] = None
    registration_end: Optional[datetime] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    submission_deadline: Optional[datetime] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class CompetitionList(BaseModel):
    """竞赛列表项，包含基本信息"""
    id: int
    title: str
    short_description: Optional[str] = None
    type: CompetitionType
    category: Optional[str] = None
    tags: List[str] = []
    difficulty: DifficultyLevel
    platform: Optional[str] = None
    prize_pool: Optional[str] = None
    status: CompetitionStatus
    participants_count: int
    teams_count: int
    image_url: Optional[str] = None  # 原始图片URL
    banner_image: Optional[str] = None  # 处理后的图片URL
    is_featured: bool
    end_date: datetime
    created_at: datetime

    class Config:
        from_attributes = True


class CompetitionFilter(BaseModel):
    """竞赛筛选参数"""
    search: Optional[str] = None
    type: Optional[CompetitionType] = None
    category: Optional[str] = None
    difficulty: Optional[DifficultyLevel] = None
    platform: Optional[str] = None
    status: Optional[CompetitionStatus] = None
    is_featured: Optional[bool] = None
    tags: Optional[List[str]] = None
    page: int = Field(1, ge=1)
    size: int = Field(20, ge=1, le=100)